package com.ali.ttt.analysis.controller;

import cn.hutool.core.util.StrUtil;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.index.query.*;
import com.ali.ttt.analysis.entity.AccessOperateRecord;
import com.ali.ttt.analysis.server.AccessOperateRecordServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexedObjectInformation;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author : yuxl
 * @create 2024/7/17 5:40 PM
 */
@Slf4j
@RestController
@RequestMapping("/analysis/operateRecord")
@Api(tags = "访问操作记录")
@AllArgsConstructor
public class AccessOperateRecordController {

    private final ElasticsearchRestTemplate elasticsearchRestTemplate;
    private final AccessOperateRecordServer accessOperateRecordServer;

    @PostMapping("/createIndex")
    @ApiOperation(value = "创建一个索引")
    public boolean createIndex() {
        return elasticsearchRestTemplate.indexOps(AccessOperateRecord.class).create();
    }


    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public AccessOperateRecord add(@RequestBody AccessOperateRecord entity) throws IOException {
        return elasticsearchRestTemplate.save(entity);
    }

    @PostMapping("/addBash")
    @ApiOperation(value = "批量新增")
    public void addBash(@RequestBody ArrayList<AccessOperateRecord> list){
        accessOperateRecordServer.saveAll(list);
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public void update(@RequestBody AccessOperateRecord entity){
        elasticsearchRestTemplate.save(entity);
    }

    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除")
    public void deleteById(@PathVariable String id){
        accessOperateRecordServer.deleteById(id);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public void delete(@RequestBody AccessOperateRecord entity){
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        if(StrUtil.isNotEmpty(entity.getName())){
            builder.filter().add(QueryBuilders.nestedQuery("user",QueryBuilders.termQuery("name",entity.getUser().getName()), ScoreMode.None));
        }
        NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(builder).build();
        log.info("查询:{}", Objects.requireNonNull(elasticsearchRestTemplate.delete(query,AccessOperateRecord.class)));
        elasticsearchRestTemplate.delete(query,AccessOperateRecord.class);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询")
    public List<SearchHit<AccessOperateRecord>> list(@RequestBody AccessOperateRecord entity){
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        if(StrUtil.isNotEmpty(entity.getName())){
            builder.filter().add(QueryBuilders.termQuery("name",entity.getName()));
        }
        if(entity.getUser()!=null){
            if(entity.getUser().getName()!=null){
                builder.filter().add(QueryBuilders.nestedQuery("user",QueryBuilders.termQuery("user.name",entity.getUser().getName()),ScoreMode.None));
            }
        }
        NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(builder).build();
        log.info("查询:{}", Objects.requireNonNull(query.getQuery()));
        return elasticsearchRestTemplate.search(query, AccessOperateRecord.class).toList();
    }
}
